package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbAddressMapper;
import com.dongdongshop.mapper.TbAreasMapper;
import com.dongdongshop.mapper.TbCitiesMapper;
import com.dongdongshop.mapper.TbProvincesMapper;
import com.dongdongshop.model.*;
import com.dongdongshop.service.AddressService;
import com.dongdongshop.vo.AddressVO;
import com.dongdongshop.vo.AreasVO;
import com.dongdongshop.vo.CitiesVO;
import com.dongdongshop.vo.ProvincesVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AddressServiceImpl implements AddressService {
    @Resource
    private TbAddressMapper addressMapper;
    @Resource
    private TbProvincesMapper provincesMapper;
    @Resource
    private TbCitiesMapper citiesMapper;
    @Resource
    private TbAreasMapper areasMapper;
    @Override
    public List<AddressVO> getAddressByUserId(String userId) {
        TbAddressExample example = new TbAddressExample();
        TbAddressExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<TbAddress> tbAddresses = addressMapper.selectByExample(example);
        List<AddressVO> collect = tbAddresses.stream().map(tb -> {
            AddressVO vo = new AddressVO();
            BeanUtils.copyProperties(tb, vo);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public AddressVO getAddressById(Long addressId) {
        TbAddress tbAddress = addressMapper.selectByPrimaryKey(addressId);
        AddressVO addressVO = new AddressVO();
        BeanUtils.copyProperties(tbAddress,addressVO);
        return addressVO;
    }

    @Override
    public List<ProvincesVO> getAllProvince() {
        List<TbProvinces> tbProvinces = provincesMapper.selectByExample(null);
        List<ProvincesVO> collect = tbProvinces.stream().map(p -> {
            ProvincesVO vo = new ProvincesVO();
            BeanUtils.copyProperties(p, vo);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<CitiesVO> getAllCity(String provinceId) {
        TbCitiesExample example = new TbCitiesExample();
        TbCitiesExample.Criteria criteria = example.createCriteria();
        criteria.andProvinceidEqualTo(provinceId);
        List<TbCities> tbCities = citiesMapper.selectByExample(example);
        List<CitiesVO> collect = tbCities.stream().map(p -> {
            CitiesVO vo = new CitiesVO();
            BeanUtils.copyProperties(p, vo);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<AreasVO> getAllArea(String cityId) {
        TbAreasExample example = new TbAreasExample();
        TbAreasExample.Criteria criteria = example.createCriteria();
        criteria.andCityidEqualTo(cityId);
        List<TbAreas> tbAreas = areasMapper.selectByExample(example);
        List<AreasVO> collect = tbAreas.stream().map(p -> {
            AreasVO vo = new AreasVO();
            BeanUtils.copyProperties(p, vo);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public void addAddress(AddressVO addressVO) {
        TbAddress address = new TbAddress();
        BeanUtils.copyProperties(addressVO,address);
        address.setCreateDate(new Date());
        address.setIsDefault("0");
        addressMapper.insertSelective(address);
    }
}
